home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / uprev / UpRev.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  4KB  |  155 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <strings.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include "UpRev_rev.h"
  12.  
  13. #define MAX_FNAME 256
  14.  
  15. const static char version_string[] = VERSTAG;
  16.  
  17. int main(int argc, char **argv)
  18. {
  19.    FILE *fp;
  20.    char *vertag;
  21.    int version;
  22.    int revision;
  23.    char fname[MAX_FNAME];
  24.    char buf[80];
  25.    time_t t;
  26.    struct tm *tp;
  27.  
  28.    if ((argc > 3) || (argc < 2))
  29.    {
  30.       printf("Usage: %s <Project> [<Version>]\n", argv[0]);
  31.       return(5);
  32.    }
  33.  
  34.    vertag = "";
  35.    version =  1;
  36.    revision = 0;
  37.  
  38.    /* Determine the name of the revision file */
  39.    strncpy(fname, argv[1], MAX_FNAME-7);
  40.    fname[MAX_FNAME-7] = 0;
  41.    strcat(fname, "_rev.h");
  42.  
  43.    /* See if the file already exists. */
  44.    fp = fopen(fname, "r");
  45.    if (fp != NULL)
  46.    {
  47.       /* It exists, get the version and revision out of it */
  48.       while(fgets(buf, 80, fp) != NULL)
  49.       {
  50.          int ln;
  51.          ln = strlen(buf)-1;
  52.          if (buf[ln] == '\n') buf[ln] = 0;
  53.  
  54.          if (!memcmp(buf, "#define ", 8))
  55.          {
  56.             char *p;
  57.             p = buf+8;
  58.             while(*p == ' ' || *p == '\t') p++;
  59.             if (!memcmp(p, "VERS ", 5))
  60.             {
  61.                p += 4;
  62.                while(*p == ' ' || *p == '\t') p++;
  63.                /* Now we need to get past the name of the executable */
  64.                ln = strlen(argv[1]);
  65.                if (strlen(p) > ln)
  66.                {
  67.                   char c;
  68.                   c = p[ln];
  69.                   p[ln] = 0;
  70.                   if (strcasecmp(p, argv[1]))
  71.                   {
  72.                      /* We don't have a match, we need to go for the */
  73.                      /* space which separates the name               */
  74.                      p[ln] = c;
  75.                      while (*p && *p != ' ') p++;
  76.                   }
  77.                   else
  78.                   {
  79.                      /* Well, it matches, shift past the name */
  80.                      p += ln + 1;
  81.                   }
  82.                   /* Now we should be pointing past the name (and a single space) */
  83.                   /* at any aux version tag information that they might have      */
  84.                   /* we want to go from the END of the string and eliminate any   */
  85.                   /* numeric digits that are there.                               */
  86.                   ln = strlen(p);
  87.                   if ((ln > 1) && (p[ln-1] == '"')) ln--;
  88.                   while((ln > 1) && (p[ln-1] >= '0') && (p[ln-1] <= '9')) ln--;
  89.                   if ((ln > 1) && (p[ln-1] == '.'))
  90.                   {
  91.               ln--;
  92.                   while((ln > 1) && (p[ln-1] >= '0') && (p[ln-1] <= '9')) ln--;
  93.                   }
  94.                   p[ln] = 0;
  95.                   while (*p == ' ' || *p == '\t') p++;
  96.                   vertag = strdup(p);
  97.                }
  98.             }
  99.             else if (!memcmp(p, "VERSION ", 8))
  100.             {
  101.                version = atoi(p+7);
  102.             }
  103.             else if (!memcmp(p, "REVISION ", 9)) revision = atoi(p+8);
  104.          }
  105.       }
  106.  
  107.       fclose(fp);
  108.    }
  109.  
  110.    revision++;
  111.  
  112.    /* Figure out what version number we will be using */
  113.    if (argc > 2)
  114.    {
  115.       int oldver = version;
  116.       version = atoi(argv[2]);
  117.       if (version != oldver)
  118.          revision = 0;
  119.    }
  120.  
  121.    fp = fopen(fname, "w");
  122.    if (fp == NULL)
  123.    {
  124.       perror(fname);
  125.       return(20);
  126.    }
  127.  
  128.    time(&t);
  129.    tp = localtime(&t);
  130.  
  131.    (void)tp->tm_mday;
  132.    (void)tp->tm_year;
  133.    (void)tp->tm_mon;
  134.  
  135.    sprintf(buf, "%d.%d.%d", tp->tm_mday, tp->tm_mon+1, tp->tm_year);
  136.  
  137.    /* Figure out what is the number to */
  138.  
  139.    fprintf(fp, "#define VERSION        %d\n",
  140.                                        version);
  141.    fprintf(fp, "#define REVISION       %3d\n",
  142.                                        revision);
  143.    fprintf(fp, "#define DATE    \"%s\"\n",
  144.                                   buf);
  145.    fprintf(fp, "#define VERS    \"%s %s%d.%d\"\n",
  146.                                  argv[1], vertag, version, revision);
  147.    fprintf(fp, "#define VSTRING \"%s %s%d.%d (%s)\"\n",
  148.                                  argv[1], vertag, version, revision, buf);
  149.    fprintf(fp, "#define VERSTAG \"\\0$%s: %s %s%d.%d (%s)\"\n",
  150.                                  "VER", argv[1], vertag, version, revision, buf);
  151.    fclose(fp);
  152.  
  153.    return(0);
  154. }
  155.